package com.smartStatus.core.util;


import com.smartStatus.core.api.VerficationCode;
import lombok.extern.slf4j.Slf4j;
import sun.awt.image.BufferedImageGraphicsConfig;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * @ClassName : VerfiCodeConfig
 * @Author : lby
 * @Date: 2020/11/30 13:33
 * @Description : 验证码生成
 */
@Slf4j
public class CodeUtils {

    /**
     * 公共随机数
     */
    private final static Random RANDOM = new Random();

    /**
     * 验证码生成
     */
    public static BufferedImage codeGeneration() {
        /*生成图片*/
        BufferedImage image = new BufferedImage(VerficationCode.IMG_WIDTH, VerficationCode.IMG_HEIGH, BufferedImage.TYPE_INT_RGB);
        /*设置图片属性*/
        BufferedImageGraphicsConfig config = BufferedImageGraphicsConfig.getConfig(image);
        image = config.createCompatibleImage(VerficationCode.IMG_WIDTH, VerficationCode.IMG_HEIGH, Transparency.TRANSLUCENT);
        Graphics graphics = image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.setClip(0, 0, VerficationCode.IMG_WIDTH, VerficationCode.IMG_HEIGH);
        graphics.drawRect(0, 0, VerficationCode.IMG_WIDTH - 2, VerficationCode.IMG_HEIGH - 2);
//        添加干扰线
        for (int i = 0; i < VerficationCode.DISTURB_LINE_SIZE; i++) {
            addDisturbLine(graphics);
            addDisturbLineAgain(graphics);
        }
//        在画布上写算式
        getRandomFormula();
//        生成随机保存字符串
        StringBuffer logsu = new StringBuffer();
        for (int j = 0, k = VerficationCode.RANDOM.length(); j < k; j++) {
            int chid = 0;
            if (j == 1) {
                chid = Integer.parseInt(VerficationCode.RULES.get(String.valueOf(VerficationCode.RANDOM.charAt(j))));
            } else {
                chid = Integer.parseInt(String.valueOf(VerficationCode.RANDOM.charAt(j)));
            }
            String ch = String.valueOf(VerficationCode.EXPRESSION.charAt(chid));
//            logsu.append(ch);
            drawRandomString((Graphics2D) graphics, ch, j);
        }
        drawRandomString((Graphics2D) graphics, "=?", 3);
        logsu.append(VerficationCode.RANDOM);
        logsu.append("=?");
        log.info("汉字验证码:{}", logsu);
        VerficationCode.RANDOM = logsu.toString();
        graphics.dispose();
        return image;
    }

    /**
     * 添加干扰线
     */
    public static void addDisturbLine(Graphics graphics) {
        int x1 = RANDOM.nextInt(VerficationCode.IMG_WIDTH);
        int y1 = RANDOM.nextInt(VerficationCode.IMG_HEIGH);
        int x2 = RANDOM.nextInt(13);
        int y2 = RANDOM.nextInt(15);
        graphics.drawLine(x1, y1, x1 + x2, y1 + y2);
    }

    public static void addDisturbLineAgain(Graphics graphics) {
        int x1 = RANDOM.nextInt(VerficationCode.IMG_WIDTH);
        int y1 = RANDOM.nextInt(VerficationCode.IMG_HEIGH);
        int x2 = RANDOM.nextInt(13);
        int y2 = RANDOM.nextInt(15);
        graphics.drawLine(x1, y1, x1 - x2, y1 - y2);
    }

    /**
     * 验证码算式
     */
    public static void getRandomFormula() {
        int x = RANDOM.nextInt(10);
        int y = RANDOM.nextInt(10);
        StringBuilder builder = new StringBuilder();
        /*随机生成0-1之间的浮点数，四舍六入五随机*/
        int round = (int) Math.round(Math.random() * 2);
        /*四则运算*/
        if (round == 0) {
            VerficationCode.CODE_RESULT = x * y;
            builder.append(x);
            builder.append("*");
            builder.append(y);
        } else if (round == 1) {
            VerficationCode.CODE_RESULT = y + x;
            builder.append(y);
            builder.append("+");
            builder.append(x);
        } else if (round == 2) {
            VerficationCode.CODE_RESULT = y - x;
            builder.append(y);
            builder.append("-");
            builder.append(x);
            //add
        } else {
            VerficationCode.CODE_RESULT = y + x;
            builder.append(y);
            builder.append("+");
            builder.append(x);
        }
        VerficationCode.RANDOM = builder.toString();
    }

    /**
     * 随机字符
     */
    public static void drawRandomString(Graphics2D g, String randomvcch, int i) {
        g.setFont(VerficationCode.FONT);
        int rc = RANDOM.nextInt(255);
        int gc = RANDOM.nextInt(255);
        int bc = RANDOM.nextInt(255);
        g.setColor(new Color(rc, gc, bc));
        int x = RANDOM.nextInt(3);
        int y = RANDOM.nextInt(2);
        g.translate(x, y);
        int degree = new Random().nextInt() % 15;
        g.rotate(degree * Math.PI / 180, 5 + i * 25, 20);
        g.drawString(randomvcch, 5 + i * 20, 25);
        g.rotate(-degree * Math.PI / 180, 5 + i * 25, 20);
    }

}



